<template>
    <div class="collect-list" v-loading="loading">
        <div class="collect-option-box">
            <div class="input-box">
                <el-button type="success" class="btn" style="margin-left: 0;" @click="goCollectDetail({ targetType: 1 })">新建视频活动</el-button>
                <el-button type="success" class="btn" @click="goCollectDetail({ targetType: 0 })">新建游戏单活动</el-button>
            </div>
        </div>
        <div class="table-box">
            <el-table :data="tableData" ref="table" @sort-change="sortChange" border style="width: 100%" :span-method="tableMerge">
                <el-table-column min-width="80" prop="activityId" label="ID"></el-table-column>
                <el-table-column min-width="120" prop="title" label="征集活动名称"></el-table-column>
                <el-table-column min-width="200" prop="subTitle" label="副标题"></el-table-column>
                <el-table-column min-width="100" prop="currentTimes" label="征集内容">
                    <template slot-scope="scope">
                        <div v-if="scope.row.targetType">视频</div>
                        <div v-else>游戏单</div>
                    </template>
                </el-table-column>
                <el-table-column width="180" label="活动时间">
                    <template slot-scope="scope">
                        <div>{{ scope.row.startTime }} - {{ scope.row.endTime }}</div>
                    </template>
                </el-table-column>
                <el-table-column min-width="100" prop="raceName" label="赛道"></el-table-column>
                <el-table-column min-width="100" label="征集对象">
                    <template slot-scope="scope">
                        <div v-if="scope.row.joinType === 1">
                            <span>创作者</span>
                        </div>
                        <div v-if="scope.row.joinType === 0">
                            <span>趣游星人</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="奖励类型" width="100">
                    <template slot-scope="scope">
                        <div v-if="scope.row.auditType === 1">
                            <span>人工审核</span>
                        </div>
                        <div v-if="scope.row.auditType === 0">
                            <span>打榜</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column min-width="100" prop="joinCount" label="投稿数"></el-table-column>
                <el-table-column min-width="100" prop="joinUserCount" label="参赛人数"></el-table-column>
                <el-table-column min-width="100" prop="status" label="状态">
                    <template slot-scope="scope">
                        <div style="color:#F56C6C;" v-if="scope.row.status === 0">
                            <span>未上架</span>
                        </div>
                        <div style="color: #67C23A;" v-else-if="scope.row.status === 1">
                            <span>已上架</span>
                        </div>
                        <div v-else>
                            <span style="color: #67C23A;">已结束</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column fixed="right" label="操作">
                    <template slot-scope="scope">
                        <div v-if="scope.row.status === 1">
                            <el-button @click="changeStatus(scope.row, 0)" type="text" size="small">下架</el-button>
                        </div>
                        <div v-if="scope.row.status === 0">
                            <el-button @click="changeStatus(scope.row, 1)" type="text" size="small">上架</el-button>
                        </div>
                        <div v-if="scope.row.status === -1">
                            <el-button @click="goCollectDetail({ id: scope.row.activityId, status: -1, targetType: scope.row.targetType })" type="text" size="small">查看</el-button>
                        </div>
                        <div v-else>
                            <el-button @click="goCollectDetail({ id: scope.row.activityId, targetType: scope.row.targetType })" type="text" size="small">编辑</el-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="tools">
            <el-pagination
                @size-change="sizeChange"
                @current-change="pageChange"
                :current-page="pageNum"
                :page-sizes="_pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
import { formatDate, tableFormat } from '@/utils/utils'
export default {
    data () {
        return {
            paramsList: {
                phase: '',
                rewardStatus: '',
                endTime: null,
                exchangeStatus: null,
                gameId: '',
                activityId: ''
            },
            tableData: [],
            pageNum: 1,
            pageSize: this._pageSize,
            total: 0,
            loading: false,
            gameLoading: false, // 查询游戏的loading
            gameList: [] // 游戏列表
        }
    },
    activated () {
        if (location.host.indexOf('work.duodian.cn') !== -1) {
            this.gameList = this._proGameList
        } else {
            this.gameList = this._devGameList
        }
        this.getList(this.pageNum)
    },
    methods: {
        // 获取列表
        async getList (pageNum) {
            if (this.loading) return
            this.pageNum = pageNum
            this.loading = true
            let params = {
                pageNum: pageNum,
                pageSize: this.pageSize
            }
            let res = await this.$http.get('/gameList/selectCollectionList', { params })
            if (res.code === '0') {
                res.data.map(item => {
                    item.endTime = formatDate(item.endTime, 'YYYY-MM-DD HH:MM:SS')
                    item.startTime = formatDate(item.startTime, 'YYYY-MM-DD HH:MM:SS')
                })
                this.tableData = tableFormat(res.data, 'collectionRaceDtoList', [
                    'raceName', 'joinType', 'auditType', 'joinCount', 'joinUserCount'
                ], {
                    joinCount: '0',
                    joinUserCount: '0'
                })
                console.log(this.tableData)
                this.total = res.count
            } else {
                this.$message({
                    type: 'error',
                    message: '系统异常，请稍后再试'
                })
            }
            this.loading = false
        },
        tableMerge ({ row, column, rowIndex, columnIndex }) {
            if (row.merge >= 0 && (columnIndex >= 10 || columnIndex <= 4)) {
                if (row.merge === 0) {
                    return {
                        rowspan: 0,
                        colspan: 0
                    }
                } else {
                    return {
                        rowspan: row.merge,
                        colspan: 1
                    }
                }
            }
        },
        // 后端排序
        sortChange ({ column, prop, order }) {
            this.sort = order || ''
            // 如果没有 sort prop 置为空
            if (!this.sort) {
                this.type = ''
            } else {
                this.type = prop
            }
            this.getList(1)
        },

        selectChange () {
            this.type = ''
            this.sort = ''
            this.$refs.table.clearSort()
            this.getList(1)
        },

        // 页码切换
        sizeChange (val) {
            this.pageNum = 1
            this.pageSize = val
            this.getList(1)
        },

        // 页容切换
        pageChange (val) {
            this.pageNum = val
            this.getList(this.pageNum)
        },

        // 查询游戏
        filterMethod (res) {
            clearTimeout(this.timerId)
            if (!res) {
                this.gameLoading = false
                return
            }
            this.gameLoading = true
            this.timerId = setTimeout(() => {
                this.getgameList(res)
            }, 1000)
        },
        async getgameList (keyword) {
            let params = {
                keyword,
                pageIndex: 1,
                pageNum: 100
            }
            let res = await this.$http.post('/home/findGames', params)
            if (res.code === '0') {
                this.gameList = res.data
            } else {
                this.$message({
                    type: 'error',
                    message: '系统异常，请稍后再试'
                })
            }
            this.gameLoading = false
        },
        changeStatus (item, status) {
            let params = {
                id: item.activityId,
                status
            }
            this.$http.get('/gameList/updateActivityStatus', { params }).then(res => {
                if (res.code === '0') {
                    let message = status === 0 ? '下架成功' : '上架成功'
                    item.status = status
                    this.$message({
                        type: 'success',
                        message
                    })
                } else if (res.code === '80011') {
                    this.$message({
                        type: 'warning',
                        message: '无权限操作'
                    })
                } else {
                    this.$message({
                        type: 'error',
                        message: '系统异常，请稍后再试'
                    })
                }
            })
        },
        goCollectDetail (query) {
            this.$router.push({
                path: '/collect/detail',
                query
            })
        // },
        // goCreate (type) {
        //     this.$router.push({
        //         path: '/collect/detail',
        //         query: {
        //             type
        //         }
        //     })
        }
    },
    filters: {
        timeInit (val) {
            if (val) {
                return formatDate(val, 'YYYY-MM-DD HH:MM:SS')
            } else {
                return '至今'
            }
        }
    }
}
</script>

<style lang="scss" scoped>
    .collect-list {
        .collect-option-box {
            max-width: 800px;
        }
        .input-box {
            margin-top: 10px;
            font-size: 13px;
            font-weight: 500;
            .input-title {
                margin: 0 10px 0 20px;
            }
            .btn {
                margin-top: 10px;
                margin-left: 10px;
            }
        }

        .table-box {
            margin: 20px 0;
            .cover {
                width: 80px;
                height: 80px;
                > img {
                    width: 100%;
                    height: 100%;
                }
            }
            .desc {
                overflow: hidden;
                line-height: 20px;
                -webkit-line-clamp: 5;
                -webkit-box-orient: vertical;
                display: -webkit-box;
                height: 100px;
            }
            .user-icon {
                width: 20px;
                height: 20px;
                border-radius: 50%;
                margin-right: 5px;
                vertical-align: top;
            }
            .table-btn {
                cursor: pointer;
                color: #409EFF;
                font-size: 13px;
                font-weight: 500;
                &:hover {
                    text-decoration: underline;
                }
            }
            .point {
                display:inline-block;
                width:10px;
                height:10px;
                border-radius: 50%;
                background-color:#67C23A;
                margin-right:3px;
            }
            .red-point {
                background-color: #F56C6C;
            }
        }

        .tools {
            padding-right: 20px;
            display: flex;
            justify-content: flex-end;
        }
    }
</style>
